import 'dart:io';


import 'package:winkai/db/dao/act_dao.dart';
import 'package:winkai/db/dao/msg_dao.dart';
import 'package:winkai/db/dao/ses_dao.dart';
import 'package:winkai/db/dao/user_dao.dart';
import 'package:winkai/db/sqflite_database_factory.dart';
import 'package:winkai/db/tables/act_tb.dart';
import 'package:winkai/db/tables/msg_tb.dart';
import 'package:winkai/db/tables/ses_tb.dart';
import 'package:winkai/db/tables/user_tb.dart';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:winkai/entity/msg_constant.dart';

part 'database.g.dart';

final rootDb = RootDatabase();

LazyDatabase _openConnection() {
  return LazyDatabase(() async {
    final filePath =
        await sqfliteDatabaseFactory.getDatabasePath('db.sqlite');
    final file = File(filePath);
    return NativeDatabase.createInBackground(file);
  });
}

@DriftDatabase(
  tables: [
    ActTb,
    UserTb,
    SesTb,
    MsgTb,
  ],
  daos: [
    ActDao,
    UserDao,
    SesDao,
    MsgDao,
  ],
)
class RootDatabase extends _$RootDatabase {
  RootDatabase() : super(_openConnection());

  @override
  int get schemaVersion => 1;

  @override
  MigrationStrategy get migration {
    return MigrationStrategy(
        onCreate: (m) async {
          await m.createAll();
        },
        onUpgrade: (m, from, to) async {});
  }
}
